package com.chris.libs.example.pages.ext.recyclerview;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.chris.libs.base.x.mvpp.mvpp.AbsStandardActivityViewProxy;
import com.chris.libs.base.x.mvpp.recycler.DefaultRvAdapter;
import com.chris.libs.example.model.DataItem;

import javax.inject.Inject;

/**
 * @author Chris chan
 * Create on： 2025/2/2 2:26
 * Use for: Fragment界面代理
 * Explain:
 * 1. 继承定义好的抽象类AbsStandardActivityViewProxy，节省代码量
 * 2. 实现契约定义好的View接口，以保持和Presenter的交互
 */
public class RecyclerViewExampleViewProxy extends AbsStandardActivityViewProxy
        implements RecyclerViewExampleContract.UIView {

    @Inject
    RecyclerViewExampleViewModel viewModel;
    @Inject
    RecyclerViewExampleViewHolders.MainViewHolder vh;
    @Inject
    RecyclerViewExamplePresenter presenter;

    private DefaultRvAdapter<DataItem, RecyclerViewExampleViewHolders.DataItemViewHolder> adapter;

    @Inject
    public RecyclerViewExampleViewProxy() {
    }

    @Override
    protected void init() {
        this.vh.bind(this.contentView);
        this.presenter.bind(this);
        this.adapter = RecyclerViewExampleAdapterManager.createAdapter(
                // 上下文
                this.context,
                // 数据列表，创建时可以传空数据
                null,
                // 列表项被点击的监听回调方法
                (tag, itemView, position, item, childView, childDataValue) -> {
                    showMessage(item.getTitle());
                });
    }

    @Override
    protected void initView() {
        this.vh.rvData.setLayoutManager(new LinearLayoutManager(this.context));
        this.vh.rvData.setAdapter(this.adapter);
    }

    @Override
    protected void setupEvent() {
        update(this.viewModel.getDataList(), dataList -> this.adapter.updateData(dataList));
    }

    @Override
    protected void onReady() {
        this.presenter.getData();
    }
}
